package com.javadroid.sgbd;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.javadroid.model.NovelaModel;

public class NovelaDao extends GenericDao{	
	
	public Integer getCountNovelasAtivas(){
		int valorMaximo = 0;
		try {
			
			String sql = "SELECT COUNT(id) FROM novelas WHERE ativo = true";
			PreparedStatement pstmt = getConnection().prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			rs.next();
			valorMaximo = rs.getInt(1);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valorMaximo;
	}
	
	
	public List<NovelaModel> getListaNovelas(){
		List<NovelaModel> novelas = new ArrayList<NovelaModel>();
		String sql = "SELECT * FROM novelas";
		try {
			PreparedStatement pstmt = getConnection().prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			
			while (rs.next()) {
				NovelaModel model = new NovelaModel();
				model.setId(rs.getInt("id"));
				model.setAtivo(rs.getBoolean("ativo"));
				model.setDescricao(rs.getString("descricao"));
				model.setEmissora(rs.getInt("emissora"));
				model.setNovela(rs.getString("novela"));
				novelas.add(model);
			}
			rs.close();
			pstmt.close();
			return novelas;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
